package com.example.user.controller.permission;

import cn.hutool.core.bean.BeanUtil;
import com.example.framework.common.pojo.R;
import com.example.user.controller.permission.vo.menu.MenuRespVO;
import com.example.user.controller.permission.vo.menu.MenuSaveVO;
import com.example.user.dal.dataobject.permission.MenuDO;
import com.example.user.service.permission.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "菜单管理")
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @PostMapping("/create")
    @Operation(summary = "创建菜单")
    @PreAuthorize("hasAuthority('system:menu:create')")
    public R<Long> createMenu(@Valid @RequestBody MenuSaveVO createReqVO) {
        Long menuId = menuService.createMenu(createReqVO);
        return R.success(menuId);
    }

    @PutMapping("/update")
    @Operation(summary = "修改菜单")
    @PreAuthorize("hasAuthority('system:menu:update')")
    public R<Boolean> updateMenu(@Valid @RequestBody MenuSaveVO updateReqVO) {
        menuService.updateMenu(updateReqVO);
        return R.success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除菜单")
    @Parameter(name = "id", description = "菜单编号", required = true, example = "1024")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    public R<Boolean> deleteMenu(@RequestParam("id") Long id) {
        menuService.deleteMenu(id);
        return R.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取菜单信息")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public R<MenuRespVO> getMenu(Long id) {
        MenuDO menu = menuService.getMenu(id);
        return R.success(BeanUtil.toBean(menu, MenuRespVO.class));
    }
}
